package com.gai.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.gai.shop.common.Const;
import com.gai.shop.entity.*;
import com.gai.shop.service.AddressService;
import com.gai.shop.service.CartService;
import com.gai.shop.service.OrdersService;
import com.gai.shop.service.impl.AddressServiceImpl;
import com.gai.shop.service.impl.CartServiceImpl;
import com.gai.shop.service.impl.OrdersServiceImpl;
import com.gai.shop.util.StrUtils;
import com.sun.mail.imap.protocol.UIDSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrdersController extends BaseController {
    private final CartService CS = new CartServiceImpl();
    private final AddressService AS = new AddressServiceImpl();
    private final OrdersService OS = new OrdersServiceImpl();

    /**
     * 获取购物车还有地址的数据，转发到order.jsp里面进行展示
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String showView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //使用uid来查询购物车的数据，收货地址的数据
        //查询用户名下说有购物车信息
        List<Cart> cartList = CS.selectCartProductByUidAndPid(user.getUid());
        List<Address> addressList = AS.selectAddressByUid(user.getUid());
        request.setAttribute(Const.UserConst.CART_LIST,cartList);
        request.setAttribute(Const.UserConst.ADD_LIST,addressList);
        return Const.FORWARD + "/order.jsp";
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //前端值给了aid
        //只需要使用uid和aid去创建一个订单数据
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //把uid和aid交给业务层去创建订单
        //在业务层处理订单
        /**
         * 1.获取uid和aid
         * 2.创建orders对象 用于保存订单信息给数据库去
         * 3.使用RandomUtils工具类生成订单编号，方便我们后面的Item数据转存
         * 4.通过uid来查找购物车的所有数据信息
         * 5.Cart转存数据到item，同时计算总金额 循环操作item保存oid值
         * 6.去数据库保存orders，在List<Item> 批量新增操作
         * 7.清空购物车数据
         */
        OS.InsertIntoOrdersByAidAndUid(aid,user.getUid());
        //创建订单完成后，去订单详情列表中展示
        return Const.FORWARD + "/order?method=showOrderList";
    }

    /**
     * 获取订单数据展示
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String showOrderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //访问业务层进行数据获取，使用参数uid
        List<Orders> ordersList = OS.selectOrdersAllByUid(user.getUid());
        request.setAttribute(Const.UserConst.ORDER_LIST,ordersList);
        return Const.FORWARD + "/orderList.jsp";
    }

    /**
     * 获取订单详情
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收参数
        String oid = request.getParameter("oid");//它是字符串
        //使用oid查询Orders数据List<Item>
        List<Item> itemList = OS.getItemByOid(oid);
        Orders orders = OS.getOrdersByOid(oid);
        //存值到request中
        request.setAttribute(Const.UserConst.ORDER,orders);
        request.setAttribute(Const.UserConst.ITEM_LIST,itemList);
        return Const.FORWARD+"/orderDetail.jsp";
    }

    public String afterPay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String oid = request.getParameter("oid");
        String result = request.getParameter("result");
        //result JSON的字符串
        WeiXin weiXin = JSON.parseObject(result, WeiXin.class);

        String res = weiXin.getResult().getResult_code();
        if (StrUtils.empty(res) && res.equals("SUCCESS")){
            //支付是成功 修改订单的状态已支付
            OS.updateOrderStateToPaySuccess(oid);
            return Const.REDIRECT + "/order?method=showOrderList";
        }else {
            request.setAttribute(Const.MESSAGE,"支付失败");
            return  Const.FORWARD + "/message.jsp";
        }
    }
}
